//
//  JYHomeViewModel.m
//  JYFarm
//
//  Created by 花花 on 2017/10/24.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYHomeViewModel.h"
#import "JYHomeBannerModel.h"
#import "JYCategorysModel.h"

@interface JYHomeViewModel()

@property (nonatomic, strong) NSMutableArray *shopcartListArray;

@property (nonatomic, strong)JYDepositObjModel *depositObjModel;

@property (nonatomic, strong)NSMutableArray *depositObjArr;

@property (nonatomic, strong)NSString  *ID;

@end
@implementation JYHomeViewModel
//轮播图
- (void)handlerBannerDataWithAreaId:(NSString *)areaId Success:(void(^)(NSArray *lbtArr))success{

    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"areaId"] = areaId;
    NSMutableArray *arr = [NSMutableArray array];
    [HTTPRequest GET:kSlideShowUrl parameter:param success:^(id resposeObject) {
        if ([resposeObject[@"code"] integerValue] == 0) {
            for (NSDictionary *dic in resposeObject[@"data"]) {
                
                JYHomeBannerModel *model = [JYHomeBannerModel mj_objectWithKeyValues:dic];
                [arr addObject:model];
            }

            success(arr);
            
        }else{
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
    }];
}



//获取商品分类接口 分类信息包含了其子类列表
- (void)handlerCategoryDataWithSuccess:(void(^)(NSArray *resposeObject))success{

       
    [HTTPRequest GET:kCategorysUrl parameter:nil success:^(id resposeObject) {
            if ([resposeObject[@"code"] integerValue] == 0) {
            
 
                for (NSDictionary *dic in resposeObject[@"data"]) {
                    
                    JYCategorysModel *model = [JYCategorysModel mj_objectWithKeyValues:dic];
                    
                    self.ID = model.id;
                    
                
                }
            success(resposeObject[@"data"]);
                
                
                
        }else{
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
    }];
}




//右边数据的处理
- (void)requsetHomeRithtTableList:(NSMutableArray *)array{

    //右边数据 **,@"categoryId":self.ID
    
    
    self.shopcartListArray = array;
    
    [self.delegate homeRightTableRequestDataSuccessWithArray:self.shopcartListArray];
    


}

//选择左边 刷新右边
- (void)SelectedProducts:(NSString *)selectedId{


    
    self.selectedId = selectedId;
    NSLog(@"选中的id%@",selectedId);
  
    
    


}
- (void)setDepositObjDic:(NSDictionary *)depositObjDic{

    _depositObjDic = depositObjDic;
    
    JYDepositObjModel *model = [JYDepositObjModel mj_objectWithKeyValues:depositObjDic];
    [self.depositObjArr addObject:model];
    
    NSLog(@"depositObjDicXXX %@",depositObjDic);
    

    
    
}

//收藏到常用清单
- (void)selectProductAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected{
  
    BaseModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    JYDepositObjModel *model = [self.depositObjArr safeObjectAtIndex:indexPath.row];

    
    
    /*
     
     如果 depositObj = null?
     {
     "list": [
     {
     "goodsAmount": 6,
     "goodsId": "03caabb155b34353a0d767946b487b44",
     "goodsName": "哈密瓜-8号XXXXX",
     "goodsPictureUrl": "https://www.baidu.com/img/baidu_jgylogo3.gif ",
     "goodsPrice": 1.0,
     "originalprice": 0.0,
     "unit": "个",
     "userId": "8f42310f-0bfc-4b79-a6f9-52b0b0adc384",
     "categroyId":"1",
    "depositObj":[
     {
     "goodsId":"2",
     "goodsName":"XXXXXXXXXXXXXXXx",
     "goodsPrice":2,
     "goodsPictureUrl":"http://pic.jinyoufarm.com/72df0411.jpg",
     "goodsDesc":null,
     "originalPrice":0,
     "goodsAmount":0,
     "unit":"斤"
     }
     ]
     
     }
     
     ]
     }
   
    */
  
    NSLog(@"-----%zd---count--%zd",productModel.amount,productModel.goodsAmount);
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
     NSMutableArray *listArr = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     dic[@"goodsAmount"] = @(productModel.amount);
        dic[@"goodsId"]  = productModel.id;
        dic[@"goodsName"] = productModel.name;
        dic[@"goodsPictureUrl"] = productModel.image;
        dic[@"goodsPrice"]  = productModel.price;
        dic[@"originalprice"] = @(20);
        dic[@"unit"] = productModel.spec;
        dic[@"userId"] = LH.userId;
        dic[@"categroyId"] = productModel.categoryId;
    
    if (model.name == nil && model.price == nil &&  model.image == nil && model.spec == nil) {

        NSLog(@"都为空表示无押金商品");
    }else{
        NSLog(@"有-有-有押金商品");
        NSMutableArray *depositObj = [NSMutableArray array];
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        dic1[@"goodsId"] = model.id;
        dic1[@"goodsName"] = model.name;
        dic1[@"goodsPrice"] = model.price;
        dic1[@"goodsPictureUrl"]  = model.image;
        dic1[@"goodsAmount"] = model.amount;
        dic1[@"unit"] = model.spec;
        [depositObj addObject:dic1];
        [dic setObject:depositObj forKey:@"depositObj"];
    }
        [listArr addObject:dic];
    
    [param setValue:listArr forKey:@"list"];
    
        [HTTPRequest POST:kAddCommonUserGreensUrl parameter:param success:^(id resposeObject) {
            
            
            NSLog(@"添加商品到常用清单 %@",resposeObject);
            if (SUCCESS) {
                
        [[NSNotificationCenter defaultCenter]postNotificationName:kHomeCountChangeNotification object:nil];
                
            }else{
                
                [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
                
            }
                        
        } failure:^(NSError *error) {
            
            [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
            
        }];
}

//取消收藏
- (void)cancelProductAtIndexPath:(NSIndexPath *)indexPath{

    BaseModel *model = [self.shopcartListArray safeObjectAtIndex:indexPath.row];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = LH.userId;
    param[@"goodsId"] = model.id;
    param[@"getDeletCommonUserOrderType"] = @(0);//0默认删除单个 1 全删 2批量
    param[@"categroyId"] = model.categoryId;
    
    [HTTPRequest POST:kDeleteCommonUserGreensUrl parameter:param success:^(id resposeObject) {
        if (SUCCESS) {
            
            [[NSNotificationCenter defaultCenter]postNotificationName:kHomeCountChangeNotification object:nil];
        }else{
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
    } failure:^(NSError *error) {
        
        
        [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
    }];

}



- (NSInteger) accountTotalCount{
    
    return 10;
    
    
    //    NSInteger totalCount = 0;
    //
    //    for (JVShopcartBrandModel *brandModel in self.shopcartListArray) {
    //        for (JVShopcartProductModel *productModel in brandModel.products) {
    //            if (productModel.isSelected) {
    //                totalCount += productModel.productQty;
    //            }
    //        }
    //    }
    //
    //    return totalCount;
}



//数量
- (void)changeCountAtIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count{
    
   
    
    BaseModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];

    
    if (count <= 0) {
        count = 1;
    } else  if (count > 10000) {
        count = productModel.amount;
    }
    //根据请求结果决定是否改变数据
    productModel.amount = count;
    
    [self adduserShoppingTrolleyListIndexPath:indexPath count:count];
    
    [self.delegate shopcartAccountFortotalCount:[self accountTotalCount]];
    
}
//添加商品到购物车
- (void)adduserShoppingTrolleyListIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count{
    
    
        
    BaseModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];
    JYDepositObjModel *model = [self.depositObjArr safeObjectAtIndex:indexPath.row];

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    NSMutableArray *listArr = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"goodsAmount"] =@(productModel.amount);
    dic[@"goodsId"]  = productModel.id;
    dic[@"goodsName"] = productModel.name;
    dic[@"goodsPictureUrl"] = productModel.image;
    dic[@"goodsPrice"]  = productModel.price;
    dic[@"originalprice"] = productModel.originalPrice;
    dic[@"unit"] = productModel.spec;
    dic[@"userId"] = LH.userId;
    dic[@"categroyId"] = productModel.categoryId;
    
    if (model.name == nil && model.price == nil &&  model.image == nil && model.spec == nil) {
        
        NSLog(@"都为空表示无押金商品");
    }else{
        NSLog(@"有-有-有押金商品");
        NSMutableArray *depositObj = [NSMutableArray array];
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        dic1[@"goodsId"] = model.id;
        dic1[@"goodsName"] = model.name;
        dic1[@"goodsPrice"] = model.price;
        dic1[@"goodsPictureUrl"]  = model.image;
        dic1[@"goodsAmount"] = model.amount;
        dic1[@"unit"] = model.spec;
        [depositObj addObject:dic1];
        [dic setObject:depositObj forKey:@"depositObj"];
    }
    [listArr addObject:dic];
    
    [param setValue:listArr forKey:@"list"];

    [HTTPRequest POST:kAdduserShoppingTrolleyUrl parameter:param success:^(id resposeObject) {
       
        NSLog(@"添加商品到购物车 %@",resposeObject);
        if (SUCCESS) {
            
            [[NSNotificationCenter defaultCenter]postNotificationName:kCountChangeNotification object:nil];
        }else{
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
            
        }
        
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
        
    }];

}
- (NSMutableArray *)shopcartListArray{

    if (!_shopcartListArray) {
        _shopcartListArray = [NSMutableArray array];
    }

    return _shopcartListArray;
    
}
- (NSMutableArray *)depositObjArr{

    if (!_depositObjArr) {
        _depositObjArr = [NSMutableArray array];
    }
    
    return _depositObjArr;
    

}
- (NSString *)jsonFromArray:(NSArray *)aArray
{
    
    NSData *data=[NSJSONSerialization dataWithJSONObject:aArray
                                                 options:NSJSONWritingPrettyPrinted
                                                   error:nil];
    
    if (data == nil) {
        
        return nil;
    }
    
    NSString *str=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    return str;
    
}

@end
